﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Tools.SQLHelper.SQLGenerator
{
    public partial class frmValidator : Form
    {
        public frmValidator()
        {
            InitializeComponent();
        }

        private void frmValidator_Load(object sender, EventArgs e)
        {
            var sb=new StringBuilder();

            foreach (var table in Tables)
            {
                sb.AppendLine($"//------------{table.Name}  开始-----------------//");
                sb.AppendLine();

                foreach (var column in table.Columns)
                {
                    if (column.IsAutoInc)
                    {
                        continue;
                    }

                    var type = T4SQLHelper.ConvertColumnTypeToNetType(column.ValueType, column.IsNullable);

                    sb.AppendLine(
                        $"var {Char.ToLowerInvariant(column.Name[0]) + column.Name.Substring(1)} = Request.GetField<{type}>(\"{column.Name}\"{(type== "DateTime" || type=="DateTime?" ? ",format:\"yyyy-MM-dd\"":"")})" +
                        $"{(column.IsNullable ? ".NotEmpty()" : "")}" +
                        $"{( type == "string" && column.Size > 0 ? $".MaxLength({column.Size})" : "")}" +
                        $".Value();");
                }

                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine($"//------------{table.Name}  结束-----------------//");

                sb.AppendLine();
                sb.AppendLine();

            }

            textBox1.Text = sb.ToStringEx();
            textBox1.SelectionStart = 0;
            textBox1.SelectionLength = 0;
        }

        public DbTableStructInfo[] Tables { set; get; }

    }
}
